const wsUrl = "ws://106.53.102.130:453/ws";

window.onload = function () {
  if ("WebSocket" in window) {
    connectStart();
  } else {
    console.log("WebSocket not in window");
  }
};

function connectStart() {
  let websocket = new WebSocket(wsUrl);
  websocket.onerror = function () {
    console.log("websocket error");
  };
  websocket.onopen = function (event) {
    console.log("websocket open");
  };
  websocket.onmessage = function (event) {
    console.log("websocket on message ", event.data);
    if (event.data == "update") {
      //刷新网页
      window.location.reload();
    }
  };

  //连接关闭的回调方法
  websocket.onclose = function () {
    console.log("websocket close,retry after 3s");
    setTimeout(connectStart, 3000);
  };
}
